﻿using System;
using NENAData.Errors;
using NENAData.LINQ;
using NENAData.NENAMember;
using NENAData.NENAMember.Account;
using NENAData.Transactions.Payments.PayPal;
using NENAData.Payments;

namespace PayPal
{
    public partial class __OrderConfirmation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                OrderConfirmation();
            }
            catch (Exception ex)
            {
                ErrHandler.WriteError("In OrderConfirmation: Form_Load: <br/>" + ex.Message);
                Response.Write("Sorry, an Error Occurred <br />" + ex.InnerException + "<br />" + ex.Message + "<br />");
            }
        }

        protected void OrderConfirmation() 
        {
            try
            {

                NVPAPICaller NENANVP = new NVPAPICaller();

                string retMsg = "";
                string strtoken = "";
                string finalPaymentAmount = "";
                string payerId = "";
                int intAffiliateID = Convert.ToInt32(Session["Affiliate_ID"].ToString());
                //NVPCodec decoder;
                var decoder = new NVPCodec();


                strtoken = Request.QueryString["token"] == "" ? Session["token"].ToString() : Request.QueryString["token"];
                payerId = Request.QueryString["PayerId"] == "" ? Session["PayerId"].ToString() : Request.QueryString["PayerId"];

                finalPaymentAmount = Session["payment_amt"].ToString();
                Response.Write("payment_amt: " + finalPaymentAmount);
                Response.Write("<br />payerId: " + payerId);
                Response.Write("<br />token: " + strtoken);

                bool boolSuccess = NENANVP.ConfirmPayment(finalPaymentAmount, "Membership Fees", strtoken, payerId, ref decoder, ref retMsg, intAffiliateID);

                Response.Write("<br />Amount: " + decoder["PAYMENTINFO_0_AMT"]);
                Response.Write("<br />Currency: " + decoder["PAYMENTINFO_0_CURRENCYCODE"]);
                Response.Write("<br />transactionId: " + decoder["PAYMENTINFO_0_TRANSACTIONID"]);
                Response.Write("<br />TRANSACTIONTYPE: " + decoder["PAYMENTINFO_0_TRANSACTIONTYPE"]);
                Response.Write("<br />PAYMENTTYPE: " + decoder["PAYMENTINFO_0_PAYMENTTYPE"]);
                Response.Write("<br />PAYMENTSTATUS: " + decoder["PAYMENTINFO_0_PAYMENTSTATUS"]);
                Response.Write("<br />FEEAMT: " + decoder["PAYMENTINFO_0_FEEAMT"]);
                Response.Write("<br />ORDERTIME: " + decoder["PAYMENTINFO_0_ORDERTIME"]);

                /*
                0: TOKEN
                1: SUCCESSPAGEREDIRECTREQUESTED
                2: TIMESTAMP
                3: CORRELATIONID
                4: ACK
                5: VERSION
                6: BUILD
                7: INSURANCEOPTIONSELECTED
                8: SHIPPINGOPTIONISDEFAULT
                9: PAYMENTINFO_0_TRANSACTIONID
                10: PAYMENTINFO_0_TRANSACTIONTYPE
                11: PAYMENTINFO_0_PAYMENTTYPE
                12: PAYMENTINFO_0_ORDERTIME
                13: PAYMENTINFO_0_AMT
                14: PAYMENTINFO_0_FEEAMT
                15: PAYMENTINFO_0_TAXAMT
                16: PAYMENTINFO_0_CURRENCYCODE
                17: PAYMENTINFO_0_PAYMENTSTATUS
                18: PAYMENTINFO_0_PENDINGREASON
                19: PAYMENTINFO_0_REASONCODE
                20: PAYMENTINFO_0_PROTECTIONELIGIBILITY
                21: PAYMENTINFO_0_PROTECTIONELIGIBILITYTYPE
                22: PAYMENTINFO_0_SECUREMERCHANTACCOUNTID
                23: PAYMENTINFO_0_ERRORCODE
                24: PAYMENTINFO_0_ACK
                int cntr = 0;
                foreach (var item in decoder)
	            {
                    Response.Write("<br />" + cntr + ": "  + item.ToString());
                    cntr++;
	            }
                */

                int intMemberID = 0;
                int intTransactionID = 0;
                    //ErrHandler.WriteError("BoolSuccess: " + boolSuccess + "\r\nFor TokenID: " + strtoken);
                if (boolSuccess)
                {
                    Response.Write("boolSuccess");
                    //Update the PaymentTransaction based on tokenID
                    PayPalTransactions transCheckout = new PayPalTransactions();
                    //1) mark the transaction paid
                    transCheckout.UpdateFullPayPal_byToken(strtoken, payerId, decoder, ref intMemberID, ref intTransactionID);
                    //ErrHandler.WriteError("BoolSuccess: " + boolSuccess + "\r\nFor member: " + intMemberID + "\r\n TransactionID: " + intTransactionID);
                    Response.Write("Before Activate");
                    Response.Write("<br /> intAffiliateID= " + intAffiliateID);
                    Response.Write("<br /> intTransactionID= " + intTransactionID);
                    Response.Write("<br /> intMemberID= " + intMemberID);
                    Response.Write("<br /> strtoken= " + strtoken + "<br />");

                    // 2)  process the account activation
                    Activate oActivate = new Activate();
                    oActivate.MembershipAccount(intAffiliateID, intMemberID, true, NENAPayments.PaidBy.PayPal, intTransactionID);
                    Response.Write("<br /><br /> finish activation ");
                    Response.Redirect("/paypal/ViewTransactionReciept.aspx?Member_ID=" + intMemberID + "&TransactionID=" + intTransactionID, false);
                }
                else
                {
                    Response.Write("boolSuccess= " + boolSuccess);
                    //ErrHandler.WriteError("Transaction failed for member: " + intMemberID + "\r\n TransactionID: " + intTransactionID);
                    Response.Redirect("APIError.aspx?" + retMsg, false);
                }
            }
            catch (Exception ex)
            {
                ErrHandler.WriteError("In OrderConfirmation: OrderConfirmation: <br/>" + ex.Message);
                Response.Write("Sorry, an Error Occurred During this Transaction <br /> Please try again in a few minutes as the PayPal system may not be available<br />");
                Response.Write(ex.InnerException + "<br />" + ex.Message + "<br />");
            }
        }
    }
}